<?php
if(!defined('BLOG'))exit('不作死就不会死');
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2015/1/16
 * Time: 22:32
 */
base::load_class('session_mem','',1);
class content{
    private $db;
    public function __construct(){
        $this->db = base::load_model('content_model');
    }
    public function save(){
        $forminfo = my_addslashes($_POST);
        if (!isset($_SESSION['userlogin'])) {
            echo json_encode(array(L('login_leave_article')));exit;
        }
        $this->navigation_db = base::load_model('navigation_model');
        $categroy=intval($forminfo['categroy'])-1;
        $title=$forminfo['title'];
        $content=$forminfo['content'];
        $username=strmcrypt_ex($_COOKIE['username']);
        $tagname=$forminfo['tagname'];
        $content=urldecode($content);
        $replace =array('/<script.*>/','/<\/script>/');
        $replacement = array('&lt;script&gt;','&lt;/script&gt;');
        $content=preg_replace($replace, $replacement, $content);
        $data = array(
            'typeid' => $categroy+1,
            'title' => $title,
            'content' => $content,
            'tag' => $tagname,
            'author' => $username,
            'leavetime' => time(),
        );
        $bool = $this->db->insertCached($data);
        if (!$bool) {
            echo json_encode(array(L('save_article_fail')));exit();
        }else{
            echo json_encode(array(true));exit();
        }
       /* ///处理embed视频 //匹配整个标签  0 后面再处理
       $article_id=$this->db->insertid(); //获取文章的id
        if (preg_match_all('/<embed.+height=\"(.+)\".*>/isU',$content,$embeds)) {
            $embeds= preg_replace('/height=\"([\da-z]*)\"/', '', $embeds[0]);
            $embeds= preg_replace('/width=\"([\da-z]*)\"/', '', $embeds);
            print_r($embeds);
            foreach ($embeds as $embed){
               // $this->db->insert('insert into '.PREFIX.'addfile values(null,'.$article_id.',\''.$title.'\',4,'.' \''.$embed.'\',\''.null.'\')');
            }
        }
        //处理html5 video //匹配整个标签  0
        if (preg_match_all('/<video(.+)>(.+)<\/video>/isU',$content,$html5videos)) {
            $html5videoArr=preg_replace('/height=\"([\da-z]*)\"/', '', $html5videos[0]);  //替换宽度
            $html5videoarr=preg_replace('/width=\"([\da-z]*)\"/', '',$html5videoArr);	//替换高度
            print_r($html5videoarr);
            foreach ($html5videoarr as $html5video){
                $data = array(
                    'article_id' => $article_id,
                    'title' => $title,
                    'type' => 4,
                    'path' => $html5video,
                );
                $this->db->insert($data);};
        }
        //处理图片
        preg_match_all('/<img(.*)src=\"(.+)\"(.*)>/iU', $content, $pic);  //匹配src地址  2 整个标签 0*/
    }
}